package com.ytd.common.util;

import android.Manifest;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.os.Build;

import androidx.core.app.ActivityCompat;

import com.ytd.common.base.BaseApplication;

import java.io.IOException;

/**
 * Your class notes
 *
 * @author gaoguanchao
 * @since 2023/4/14
 */
public class DeviceUtil {
    public static String getDeviceSN() {
        if (Build.VERSION.SDK_INT < Build.VERSION_CODES.O) {
            return Build.SERIAL;
        } else {
            if (ActivityCompat.checkSelfPermission(BaseApplication.getContext(),
                Manifest.permission.READ_PHONE_STATE) != PackageManager.PERMISSION_GRANTED) {
                // TODO: Consider calling
                //    ActivityCompat#requestPermissions
                // here to request the missing permissions, and then overriding
                //   public void onRequestPermissionsResult(int requestCode, String[] permissions,
                //                                          int[] grantResults)
                // to handle the case where the user grants the permission. See the documentation
                // for ActivityCompat#requestPermissions for more details.
                return Build.SERIAL;
            }
            return Build.getSerial();
        }
    }

    /**
     * 获取自己应用内部的版本号
     */
    public static int getVersionCode() {
        PackageManager manager = BaseApplication.getContext().getPackageManager();
        int code = 0;
        try {
            PackageInfo info = manager.getPackageInfo(BaseApplication.getContext().getPackageName(), 0);
            code = info.versionCode;
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
        }
        return code;
    }

    /**
     * 获取自己应用内部的版本号
     */
    public static String getVersionName() {
        PackageManager manager = BaseApplication.getContext().getPackageManager();
        String name = "1.0.0";
        try {
            PackageInfo info = manager.getPackageInfo(BaseApplication.getContext().getPackageName(), 0);
            name = info.versionName;
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
        }
        return name;
    }


    public static void shutdown() {
        //弹窗倒计时结束时，关机
//        try {
//            PowerManager pManager =
//                (PowerManager) BaseApplication.getContext().getSystemService(Context.POWER_SERVICE);
//            if (pManager != null) {
//                Method method =
//                    pManager.getClass().getMethod("shutdown", boolean.class, String.class, boolean.class);
//                method.invoke(pManager, false, null, false);
//            }
//        } catch (Exception e) {
//            e.printStackTrace();
//        }

        try {
            Runtime.getRuntime().exec("reboot -p"); //关机
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static void reboot() {
        //弹窗倒计时结束时，关机
//        try {
//            PowerManager pManager =
//                (PowerManager) BaseApplication.getContext().getSystemService(Context.POWER_SERVICE);
//            if (pManager != null) {
//                Method method =
//                    pManager.getClass().getMethod("shutdown", boolean.class, String.class, boolean.class);
//                method.invoke(pManager, false, null, false);
//            }
//        } catch (Exception e) {
//            e.printStackTrace();
//        }

        try {
            Runtime.getRuntime().exec("reboot"); //关机
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
